<?php 
namespace Marketing\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Predicate\Like;

class MarketingTable extends AbstractTableGateway {

    protected $table = 'marketinglist';

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
    }
    
    public function fetchAll() {
        $resultSet = $this->select(function (Select $select) {
                    $select->order('id ASC');
                });
        $entities = array();
        foreach ($resultSet as $row) {
            $entity = new Entity\Marketinglist();
            $entity->setId($row->id)
                    ->setName($row->name)
                    ->setDescription($row->description)
                    ->setFromName($row->fromname)
                    ->setFromAddress($row->fromaddress)
                    ->setAnyOneCanSubscribe($row->anyonecansubscribe);
            $entities[] = $entity;
        }
        return $entities;
    }

    public function getMarketinglist($id) {
        $row = $this->select(array('id' => (int) $id))->current();
        if (!$row)
            return false;

        $Marketinglist = new Entity\Marketinglist(array(
                    'id' => $row->id,
                    'name' => $row->name,
                    'description' => $row->description,
                    'fromname' => $row->fromname,
                    'fromaddress' => $row->fromaddress,
                    'anyonecansubscribe' => $row->anyonecansubscribe,
                ));
        return $Marketinglist;
    }
    
    public function fetchDetail() {
    	$Marketinglist = new Entity\Marketinglist();
    	$id = (int) $Marketinglist->getId();
    	
    	$resultSet = $this->select(function (Select $select){
    		$select->where('id' == 1);
    	});
    	/*$spec = $this->where(function (Where $where){
    			$where->like('id',$id);
    	});	
    	$resultSet = $select->where($spec); */
    
    	$entities = array();
    	foreach ($resultSet as $row) {
    		$Marketinglist->setId($row->id)
	    		->setName($row->name)
	    		->setDescription($row->description)
	    		->setFromName($row->fromname)
	    		->setFromAddress($row->fromaddress)
	    		->setAnyOneCanSubscribe($row->anyonecansubscribe);
    		$entities[] = $Marketinglist;
    }
    	return $entities;
    }

    public function saveMarketinglist(Entity\Marketinglist $Marketinglist) {
        $data = array(
            'name' => $Marketinglist->getName(),
            'description' => $Marketinglist->getDescription(),
            'fromname' => $Marketinglist->getFromName(),
            'fromaddress' => $Marketinglist->getFromAddress(),
            'anyonecansubscribe' => $Marketinglist->getAnyOneCanSubscribe(),
        	'ownedsecurableitem_id'  => $Marketinglist->ownedsecurableitem_id,
        );
        

        $id = (int) $Marketinglist->getId();

        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
        	if ($this->getMarketinglist($id)) {
        		$this->tableGateway->update($data, array('id' => $id));
        } else {
        	throw new \Exception('Form id does not exist');
        }

    	}
    }

    public function removeMarketinglist($id) {
        return $this->delete(array('id' => (int) $id));
    }
    
}

?>